#!/usr/bin/env bash

if [[ 2 -ne $# ]]; then
    printf "\x1b[31;01mUsage:\n\t \$CMD \$1 \$2\x1b[00m\n"
    exit 1
fi

if [[ 0 -eq `echo $0 | grep -c '^/'` ]]; then
    # relative path
    EXEC_PATH=$(dirname "`pwd`/$0")
else
    # absolute path
    EXEC_PATH=$(dirname "$0")
fi

cd $EXEC_PATH || exit 1

include_params=$1
lib_params=$2
unit=`basename $0 | grep -o '^[^.]\+'`
CC=`which clang` || CC=`which gcc` || exit 1
CCFLAGS='-g -std=c99 -Wall -Wextra -fvisibility=hidden -Wl,--as-needed'

$CC $CCFLAGS ${unit}.c -lpthread $include_params $lib_params -g -o a.out
if [[ 0 -ne $? ]]; then
    printf "\x1b[31;01mcompile failed!\x1b[00m"
    exit 1
fi

# start...
./a.out

